package io.renren.modules.weixin.Service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.weixin.Service.ZbAdminService;
import io.renren.modules.weixin.Service.ZbResultService;
import io.renren.modules.weixin.dao.ZbAdminDao;
import io.renren.modules.weixin.entity.ComputeEntity;
import io.renren.modules.weixin.entity.StatisticsEntity;
import io.renren.modules.weixin.entity.ZbAdminEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: Mr-wang
 * @date: 2018/4/20 0020 11:28
 * @Description:
 */
@Service("zbAminService")
public class ZbAdminServiceImpl extends ServiceImpl<ZbAdminDao, ZbAdminEntity> implements ZbAdminService {
   @Autowired
   private ZbResultService zbResultService;
   @Autowired
   private ZbAdminService zbAdminService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String creator = (String)params.get("creator");
        String zbName = (String)params.get("zbName");

        Page<ZbAdminEntity> page = this.selectPage(
                new Query<ZbAdminEntity>(params).getPage(),
                new EntityWrapper<ZbAdminEntity>().where("creator = {0}",creator)
                        .like(StringUtils.isNotBlank(zbName),"zb_name",zbName)
        );
        return new PageUtils(page);
    }

    @Override
    public void configzb(ZbAdminEntity zbparams){

        ZbAdminEntity zb = new ZbAdminEntity();
        ComputeEntity c = zbResultService.selectByZbCode(zbparams.getZbCode());
        zb.setZbCode(zbparams.getZbCode());
        zb.setZbName(c.getZbName());
        double minv = (c.getAvgValue() - c.getMinValue())/10;
        zb.setMinValue(c.getMinValue() + minv);
        double maxv = (c.getMaxValue() - c.getAvgValue())/10;
        zb.setMaxValue(c.getMaxValue() - maxv);
        //最新记录
        zb.setZbAvgValue(c.getAvgValue());
        zb.setZbMaxValue(c.getMaxValue());
        zb.setZbMinValue(c.getMinValue());
        String level = zbparams.getZbLevel();
        zb.setZbLevel(level);
        zb.setStatus("off");
        zb.setCreator(zbparams.getCreator());
        zb.setRemark(zbparams.getRemark());
        zbAdminService.insert(zb);
    }

    @Override
    public void deleteByzbCodeAndcreator(String zbCode, String creator) {
        baseMapper.deleteByzbCodeAndcreator(zbCode, creator);
    }

    @Override
    public List<ZbAdminEntity> selectByzbCode(String zbCode) {
        return baseMapper.selectByzbCode(zbCode);
    }

    @Override
    public List<ZbAdminEntity> selectBycreator(String creator) {
        return baseMapper.selectBycreator(creator);
    }

    @Override
    public ZbAdminEntity selectByzbCodeAndcreator(String zbCode, String creator) {
      return baseMapper.selectByzbCodeAndcreator(zbCode, creator);
    }

    @Override
    public void updateStatusOff(String creator) {
        baseMapper.updateStatusOff(creator);
    }

    @Override
    public void updateStatusOn(String creator) {
        baseMapper.updateStatusOn(creator);
    }

    @Override
    public void updateByzbCodeAndcreator(ZbAdminEntity zbparams) {
        double minValue = zbparams.getMinValue();
        double maxValue = zbparams.getMaxValue();
        String zbLevel = zbparams.getZbLevel();
        String remark = zbparams.getRemark();
        String zbCode = zbparams.getZbCode();
        String creator = zbparams.getCreator();
        baseMapper.updateByzbCodeAndcreator(minValue, maxValue, zbLevel, remark, zbCode, creator);
    }

    @Override
    public void updateOkByzbCodeAndcreater(int ok, String zbCode, String creator) {
        baseMapper.updateOkByzbCodeAndcreater(ok,zbCode,creator);
    }

    @Override
    public void updateErrorByzbCodeAndcreater(int error, String zbCode, String creator) {
        baseMapper.updateErrorByzbCodeAndcreater(error,zbCode,creator);
    }

    @Override
    public void updateAlertByzbCodeAndcreater(int alert, String zbCode, String creator) {
        baseMapper.updateAlertByzbCodeAndcreater(alert,zbCode,creator);
    }

    @Override
    public void updateWarningByzbCodeAndcreater(int warning, String zbCode, String creator) {
        baseMapper.updateWarningByzbCodeAndcreater(warning,zbCode,creator);
    }

    @Override
    public void updateStatusOpen(String zbCode, String creator) {
        baseMapper.updateStatusOpen(zbCode,creator);
    }

    @Override
    public void updateStatusClose(String zbCode, String creator) {
        baseMapper.updateStatusClose(zbCode,creator);
    }

    @Override
    public StatisticsEntity statisticsAdmin(String creator) {
        return baseMapper.statisticsAdmin(creator);
    }

    @Override
    public List<ZbAdminEntity> selectList() {
        return baseMapper.selectList();
    }
}